### This is the R code used to create all of the figures in the paper ###
### using the raw data, which was imported from an excel file.        ###

### Create a vector of the R packages needed to replicate the data analysis and create the graphs
nsf_packages <- c("ggplot2", "wesanderson", "plyr", "tidyverse", "readxl", "writexl", "ggpattern", 
                  "viridis", "RColorBrewer", "scales","lubridate","dplyr")  
lapply(nsf_packages, require, character.only = TRUE)    ### Load multiple packages

###  Initial data setup, creating required variables
nsf.df<-read_excel("Moustafa_NSF.xlsx", col_names=TRUE) 
nsf.df$Date<-ymd(nsf.df$Start) ### Creating a new Date variable using lubridate

### Creating five-year periods centered around the mid-point XXX2.5 and XXX7.5
nsf.df <- nsf.df %>%
  select(names(nsf.df)) %>%
  mutate(
    five.year = ifelse(Date <= ymd("1965-12-31"), 1962.5, 
                       ifelse(Date <= ymd("1970-12-31"), 1967.5,
                       ifelse(Date <= ymd("1975-12-31"), 1972.5,
                       ifelse(Date <= ymd("1980-12-31"), 1977.5,
                       ifelse(Date <= ymd("1985-12-31"), 1982.5,
                       ifelse(Date <= ymd("1990-12-31"), 1987.5,
                       ifelse(Date <= ymd("1995-12-31"), 1992.5,
                       ifelse(Date <= ymd("2000-12-31"), 1997.5,
                       ifelse(Date <= ymd("2005-12-31"), 2002.5,
                       ifelse(Date <= ymd("2010-12-31"), 2007.5,
                       ifelse(Date <= ymd("2015-12-31"), 2012.5,
                       ifelse(Date <= ymd("2020-12-31"), 2017.5, 2022.5)))))))))))))

nsf.df<-nsf.df[nsf.df$Transfer=="No",]  ## Exclude any awards that have a "Yes" in the Transfer column, which

###
### DATA PREPARATION FOR FIGURES 1 THROUGH 3 (CODE) ###
###

nsf1_3.df<-nsf.df ## Create a separate file (nsf1_3.df) just for Figures 1 through 3.

## Keeping only awards with valid methods--i.e., delete 6,7,12
keep.method<-c("1","2","3","4","5","8","9","10","11","13")
nsf1_3.df<-filter(nsf.df, Method %in% keep.method)
nsf1_3.df<-nsf1_3.df[nsf1_3.df$Type==1,] ## Keeping all only substantive project Types, i.e., type=1

### Create a new variable with method name rather than a number by recoding Method variable into Method.Name
nsf1_3.df <- nsf1_3.df %>%
  select(names(nsf1_3.df)) %>%
  mutate(
    Method.Name= factor(ifelse(Method %in% "1", "Quantitative", 
                        ifelse(Method %in% "2", "Qualitative",
                        ifelse(Method %in% "3", "Quantitative & Qualitative",
                        ifelse(Method %in% "4", "Experimental", 
                        ifelse(Method %in% "5", "Formal Theory",
                        ifelse(Method %in% "8", "Quantitative & Experimental",
                        ifelse(Method %in% "9", "Quantitative & Formal Theory","Other")))))))))

### Split amounts of Support_2021 [the amount of (2021) dollars of support for each award] 
### equally to various methods from the awards that have more than one method; 
### that is, awards coded as Method # 3,8,9,10,11,13
### The following steps are required to split the the dollars evenly between multi-method approaches
### and apportion them to the appropriate single method value.

### Create dummy variable--single method, or not
nsf1_3.df <- nsf1_3.df %>%
  select(names(nsf1_3.df)) %>%
  mutate(
    Single.Method.Dummy= ifelse(Method %in% 1, "Yes", 
                                ifelse(Method %in% 2, "Yes",
                                       ifelse(Method %in% 4, "Yes",
                                              ifelse(Method %in% 5, "Yes", "No")))))

## Separate the df into two--one for single.method; the other for multi-methods.
nsf1_3A.df<-nsf1_3.df[nsf1_3.df$Single.Method.Dummy=="Yes",]
nsf1_3B.df<-nsf1_3.df[nsf1_3.df$Single.Method.Dummy=="No",]
nsf1_3B.df <- nsf1_3B.df %>% slice(rep(1:n(), each = 2))   ### Double each of the rows in nsfB.df (i.e., multi-method file),

### Apportion the Award amount (Support_2021) evenly between the two methods in each.
### There are 6 different combinations of multi-methods

## Create new Method variable 
nsf1_3B.df$Method.1st<-as.character("")
nsf1_3B.df$Method.2nd<-as.character("")

nsf1_3B.df <- nsf1_3B.df %>%
  select(names(nsf1_3B.df)) %>%
  mutate(
    Method.1st= ifelse(Method %in% "3", "1", 
                       ifelse(Method %in% "8", "1",
                       ifelse(Method %in% "9", "1",
                       ifelse(Method %in% "10", "4", 
                       ifelse(Method %in% "11", "2", 
                       ifelse(Method %in% "13", "2", Method)))))))

nsf1_3B.df <- nsf1_3B.df %>%
  select(names(nsf1_3B.df)) %>%
  mutate(
    Method.2nd= ifelse(Method %in% "3", "2", 
                       ifelse(Method %in% "8", "4",
                       ifelse(Method %in% "9", "5",
                       ifelse(Method %in% "10", "5", 
                       ifelse(Method %in% "11", "4", 
                       ifelse(Method %in% "13", "5", Method)))))))

row_odd <- seq_len(nrow(nsf1_3B.df)) %% 2    #Creates indicator for whether the row is odd or not   
nsf1_3B.odd.df<-nsf1_3B.df[row_odd==1,] ### Now get the odd rows only
nsf1_3B.even.df<-nsf1_3B.df[row_odd==0,] ### Now get the even rows only
nsf1_3B.odd.df$Method.New<-nsf1_3B.odd.df$Method.1st  ### Create new variable Method.New with the value from Method.1 in nsfB.odd.df
nsf1_3B.even.df$Method.New<-nsf1_3B.even.df$Method.2nd ### Place the values from Method.2nd in nsfB.even.df
nsf1_3B.final.df<-rbind(nsf1_3B.odd.df, nsf1_3B.even.df)### Combine the two dfs--nsfB.odd.df and nsfB.even.df into one data frame.  
nsf1_3B.final.df$Support_2021<-nsf1_3B.final.df$Support_2021*0.5  ### Overwrite Support_2021 variable to take 1/2 of the value of each row for Support_2021

nsf1_3A.df$Method.1st<-nsf1_3A.df$Method ### Before re-attaching nsfB to nsfA, we have to add a couple of variables to nsfA.df
nsf1_3A.df$Method.2nd<-NA                ### Because there is only one method in nsfA.df
nsf1_3A.df$Method.New<-nsf1_3A.df$Method ### This is the same value.
nsf1_3.df<-rbind(nsf1_3A.df,nsf1_3B.final.df) ### Recombine nsfB.final.df with nsfA.df to make nsf.final.df

### Aggregate the data across subfield, five-year-periods, and method.
### First we have to create a new variable for method name and make it a factor variable
nsf1_3.df <- nsf1_3.df %>%
  select(names(nsf1_3.df)) %>%
  mutate(
    Method.Name= factor(ifelse(Method.New %in% "1", "Quantitative", 
                        ifelse(Method.New %in% "2", "Qualitative",
                        ifelse(Method.New %in% "4","Experimental", "Formal Theory")))))

### Because we are only going to plot American, Comparative,and IR, we'll only keep the 3 subfields
keep.subfield<-c("1","2","3") 
nsf1_3.df<-filter(nsf1_3.df, Subfield %in% keep.subfield)

### THIS NEXT CODE IS TO CREATE THE DATA FROM WHICH TO CODE 
### SUBFIELD, METHOD, AND FIVE.YEAR AND TO CREATE ZERO VALUES WHERE THIS IS NECESSARY
fig1_3.df <- as.data.frame(nsf1_3.df)  %>%
  group_by(Subfield, Method.Name, five.year, .drop=FALSE) %>%
  dplyr::summarise(Total = sum(Support_2021), .groups="drop") %>%
  complete(expand(., Subfield, Method.Name, five.year), fill = list(Total = 0)) %>%
  fill(Subfield, Method.Name,five.year)

### Re-order the Method.Name variable (creating new Method.Reorder variable) to match the order we want
fig1_3.df$Method.Reorder<-fct_relevel(fig1_3.df$Method.Name, "Quantitative","Formal Theory","Experimental","Qualitative")

### Generate custom labels for x-axis (date).
fiveyearlabels=c("1961-\n1965 ","1966-\n1970 ","1971-\n1975 ","1976-\n1980 ","1981-\n1985 ","1986-\n1990 ","1991-\n1995 ",
                 "1996-\n2000 ","2001-\n2005 ","2006-\n2010 ","2011-\n2015 ","2016-\n2020 ")

### For each of Figures 1-3, use fig1_3.df for the data, but limit each figure to one subfield--Comparative, American, IR.

####
## FIGURE 1--American Politics Subfield
####

gg.figAmerican<- ggplot(fig1_3.df[fig1_3.df$Subfield==1,], aes(x=five.year+2.5,y=Total/1000000, group=Method.Reorder, colour=Method.Reorder)) + 
  geom_line(linewidth=1) +
  geom_point(size=2, shape=18) +
  labs(x="",y="2021 USD (millions)") +
  scale_x_continuous(limits=c(1965,2020), breaks=seq(1965,2020,5), labels=fiveyearlabels) +   
  scale_y_continuous(limits = c(0, 60)) + 
  scale_color_manual(values = rev(wes_palette("IsleofDogs1", n = 4)), labels = c("Quantitative","Formal Theory","Experimental","Qualitative")) + 
  theme_bw() + 
  coord_cartesian(clip = "off") + 
  theme(legend.position = "bottom",
        legend.key.size = unit(0.4, "cm"),
        legend.title = element_blank(),
        legend.text=element_text(size=11),
        legend.box.background = element_rect(color = "black"),
        panel.grid.minor.x = element_blank(),
        panel.grid.minor.y = element_blank(),
        axis.line=element_line(),
        axis.title.y = element_text(size=10, vjust=3, color='black'),
        axis.title.x = element_text(size=11, color='black'),
        axis.text.x = element_text(color = 'black', size = 10),
        axis.text.y = element_text(size = 10, color='black'),
        plot.background = element_rect(colour="black", linewidth = 0.75),
        plot.title = element_text(face = 2, vjust = 4, hjust = 0, color='black'),
        plot.title.position = "plot",
        plot.caption.position = "plot",
        panel.spacing = unit(1, "lines"),
        plot.margin = margin(20, 20, 20, 20)) +
  geom_line(key_glyph = draw_key_rect, linewidth=0.2)
plot(gg.figAmerican)
ggsave(gg.figAmerican, filename="Figure1.png", width=8.5, height=5.7, dpi=300,units="in")  

tiff("Figure1.tiff", units="in", width=8.5, height=5.7, res=300)
plot(gg.figAmerican)
dev.off()

####
## FIGURE 2--Comparative Politics Subfield
####

gg.figComparative<- ggplot(fig1_3.df[fig1_3.df$Subfield==2,], aes(x=five.year+2.5,y=Total/1000000, group=Method.Reorder, colour=Method.Reorder)) + 
  geom_line(linewidth=0.8) +
  geom_point(size=2, shape=18) +
  labs(x="",y="2021 USD (millions)") +    
  scale_x_continuous(limits=c(1965,2020), breaks=seq(1965,2020,5), labels=fiveyearlabels) +   
  scale_y_continuous(limits = c(0, 20)) + 
  scale_color_manual(values = rev(wes_palette("IsleofDogs1", n = 4)), labels = c("Quantitative","Formal Theory","Experimental","Qualitative")) + 
  theme_bw() + 
  coord_cartesian(clip = "off") + 
  theme(legend.position = "bottom",
        legend.key.size = unit(0.4, "cm"),
        legend.title = element_blank(),
        legend.text=element_text(size=11),
        legend.box.background = element_rect(color = "black"),
        panel.grid.minor.x = element_blank(),
        panel.grid.minor.y = element_blank(),
        axis.line=element_line(),
        axis.title.y = element_text(size=10, vjust=3, color='black'),
        axis.title.x = element_text(size=11, color='black'),
        axis.text.x = element_text(color = 'black', size = 10),
        axis.text.y = element_text(size = 10, color='black'),
        plot.background = element_rect(colour="black", linewidth = 0.75),
        plot.title = element_text(face = 2, vjust = 4, hjust = 0, color='black'),
        plot.title.position = "plot",
        plot.caption.position = "plot",
        panel.spacing = unit(1, "lines"),
        plot.margin = margin(20, 20, 20, 20)) +
  geom_line(key_glyph = draw_key_rect, linewidth=0.2)
plot(gg.figComparative)
ggsave(gg.figComparative, filename="Figure2.png", width=8.5, height=5.7, dpi=300,units="in")  

tiff("Figure2.tiff", units="in", width=8.5, height=5.7, res=300)
plot(gg.figComparative)
dev.off()

####
## FIGURE 3--International Relations Subfield
####
gg.figIR<- ggplot(fig1_3.df[fig1_3.df$Subfield==3,], aes(x=five.year+2.5,y=Total/1000000, group=Method.Reorder, colour=Method.Reorder)) + 
  geom_line(linewidth=0.8) +
  geom_point(size=2, shape=18) +
  labs(x="",y="2021 USD (millions)") +  
  scale_x_continuous(limits=c(1965,2020), breaks=seq(1965,2020,5), labels=fiveyearlabels) +   
  scale_y_continuous(limits = c(0, 8)) + 
  scale_color_manual(values = rev(wes_palette("IsleofDogs1", n = 4)), labels = c("Quantitative","Formal Theory","Experimental","Qualitative")) + 
  theme_bw() + 
  coord_cartesian(clip = "off") + 
  theme(legend.position = "bottom",
        legend.key.size = unit(0.4, "cm"),
        legend.title = element_blank(),
        legend.text=element_text(size=11),
        legend.box.background = element_rect(color = "black"),
        panel.grid.minor.x = element_blank(),
        panel.grid.minor.y = element_blank(),
        axis.line=element_line(),
        axis.title.y = element_text(size=10, vjust=3, color='black'),
        axis.title.x = element_text(size=11, color='black'),
        axis.text.x = element_text(color = 'black', size = 10),
        axis.text.y = element_text(size = 10, color='black'),
        plot.background = element_rect(colour="black", linewidth = 0.75),
        plot.title = element_text(face = 2, vjust = 4, hjust = 0, color='black'),
        plot.title.position = "plot",
        plot.caption.position = "plot",
        panel.spacing = unit(1, "lines"),
        plot.margin = margin(20, 20, 20, 20)) +
  geom_line(key_glyph = draw_key_rect, linewidth=0.2)
plot(gg.figIR)
ggsave(gg.figIR, filename="Figure3.png", width=8.5, height=5.7, dpi=300,units="in")  

tiff("Figure3.tiff", units="in", width=8.5, height=5.7, res=300)
plot(gg.figIR)
dev.off()

###
###     FIGURE 4 CODE 
###

nsf4.df<-nsf.df  ### Create data for Figure 4 from main (nsf.df) data 
nsf4.df<-nsf4.df[nsf4.df$Type==1,]  ### Keep only substantive project Types, i.e., type=1
keep.subfield<-c("1","2","3")  ## Keep only American, Comparative,and IR subfields
nsf4.df<-filter(nsf4.df, Subfield %in% keep.subfield)

### THIS NEXT SECTION CREATES THE DATA FROM WHICH TO AGGREGATE DATA ACROSS 
### SUBFIELD, METHOD, AND FIVE.YEAR PERIOD
fig4.df <- as.data.frame(nsf4.df)  %>%
  group_by(Subfield, five.year, .drop=FALSE) %>%
  dplyr::summarise(Total = sum(Support_2021), .groups="drop") %>%
  complete(expand(., Subfield, five.year), fill = list(Total = 0)) %>%
  fill(Subfield,five.year)

subfield_names <- c("1" = "American Politics", "2" = "Comparative Politics", "3" = "International Relations")

gg.fig4<- ggplot(fig4.df, aes(x=five.year,y=Total/1000000)) + 
  geom_line(linewidth=0.75) +
  geom_point(size=2, shape=18) +
  facet_grid(~Subfield, labeller=as_labeller(subfield_names)) +  
  labs(x="",y="2021 USD (millions)") + 
  scale_x_continuous(limits=c(1960,2020)) + 
  scale_y_continuous(limits = c(0, 60)) + 
  scale_color_manual(values = "#000000") + 
  theme_bw() + 
  coord_cartesian(clip = "off") + 
  theme(legend.position = "bottom",
        legend.key.size = unit(0.4, "cm"),
        legend.title = element_blank(),
        legend.text=element_text(size=11),
        legend.box.background = element_rect(color = "black"),
        panel.grid.minor.x = element_blank(),
        panel.grid.minor.y = element_blank(),
        axis.line=element_line(),
        axis.title.y = element_text(size=11, vjust=3, color='black'),
        axis.title.x = element_text(size=10, color='black'),
        axis.text.x = element_text(color = 'black', size = 10),
        axis.text.y = element_text(size = 10, color='black'),
        strip.text.x = element_text(size = 11, color='black'),
        plot.background = element_rect(colour="black", linewidth=0.75),
        plot.title = element_text(face = 2, vjust = 4, hjust = 0, color='black'),
        plot.title.position = "plot",
        plot.caption.position = "plot",
        panel.spacing = unit(1, "lines"),
        plot.margin = margin(20, 20, 20, 20)) +
  geom_line(key_glyph = draw_key_rect, size=0.2)
plot(gg.fig4)
ggsave(gg.fig4, filename="Figure4.png", width=8.5, height=5.7, dpi=300,units="in")  

tiff("Figure4.tiff", units="in", width=8.5, height=5.7, res=300)
plot(gg.fig4)
dev.off()


###
### FIGURE 5 CODE
###

nsf5.df<-nsf.df ### Create data frame from main data frame
keep.method<-c("1","2","3","4","5","8","9","10","11","13")  ### Keep only awards with valid methods--i.e., delete 6,7,12
nsf5.df<-filter(nsf5.df, Method %in% keep.method)        
nsf5.df<-nsf5.df[nsf5.df$Type==2,]  ### Keep only Research Infrastructure project (Award Type--only 2)

### Create a new variable that uses the method name (not just the number)
nsf5.df <- nsf5.df %>%
  select(names(nsf5.df)) %>%
  mutate(
    Method.Name= ifelse(Method %in% "1", "Quantitative", 
                 ifelse(Method %in% "2", "Qualitative",
                 ifelse(Method %in% "3", "Quantitative &Qualitative",
                 ifelse(Method %in% "4","Experimental", 
                 ifelse(Method %in% "5", "Formal Theory", 
                 ifelse(Method %in% "8", "Quantitative &Experimental", 
                 ifelse(Method %in% "9", "Quantitative &Formal Theory", 
                 ifelse(Method %in% "10", "Experimental &Formal Theory", 
                 ifelse(Method %in% "11", "Qualitative &Experimental", "Qualitative &Formal Theory"))))))))))

## Split amounts of 2021 dollars equally to various methods from combined method values
## Create dummy variable single method or not
nsf5.df <- nsf5.df %>%
  select(names(nsf5.df)) %>%
  mutate(
    Single.Method.Dummy= ifelse(Method %in% 1, "Yes", 
                                ifelse(Method %in% 2, "Yes",
                                       ifelse(Method %in% 4, "Yes",
                                              ifelse(Method %in% 5, "Yes", "No")))))

## Separate the df into two--one for single.method; the other for multi-methods.
nsf5A.df<-nsf5.df[nsf5.df$Single.Method.Dummy=="Yes",]
nsf5B.df<-nsf5.df[nsf5.df$Single.Method.Dummy=="No",]
## Now have to double each of the rows in nsfB.df (i.e., multi-method file),
nsf5B.df <- nsf5B.df %>% slice(rep(1:n(), each = 2))

## Now we have to apportion the Award amount (2021Dollars) evenly between the two methods in each.
## But there are 6 different combinations of multi-methods
## Create new Method variable 
nsf5B.df$Method.1st<-as.character("")
nsf5B.df$Method.2nd<-as.character("")

nsf5B.df <- nsf5B.df %>%
  select(names(nsf5B.df)) %>%
  mutate(
    Method.1st= ifelse(Method %in% "3", "1", 
                ifelse(Method %in% "8", "1",
                ifelse(Method %in% "9", "1",
                ifelse(Method %in% "10", "4", 
                ifelse(Method %in% "11", "2", 
                ifelse(Method %in% "13", "2", Method)))))))

nsf5B.df <- nsf5B.df %>%
  select(names(nsf5B.df)) %>%
  mutate(
    Method.2nd= ifelse(Method %in% "3", "2", 
                ifelse(Method %in% "8", "4",
                ifelse(Method %in% "9", "5",
                ifelse(Method %in% "10", "5", 
                ifelse(Method %in% "11", "4", 
                ifelse(Method %in% "13", "5", Method)))))))

### Create variables that apportion proper award weight (1 for single method; 0.5 for multi-method)
nsf5A.df$Award.Weight<-1
nsf5B.df$Award.Weight<-0.5

### Apportion Method.1st and Method.2nd to each of the projects in nsf5B.df
### Let's split it into two, one for each duplicated project.
row_odd <- seq_len(nrow(nsf5B.df)) %% 2    #Creates indicator for whether the row is odd or not   
nsf5B.odd.df<-nsf5B.df[row_odd==1,] ### Get the odd rows only
nsf5B.even.df<-nsf5B.df[row_odd==0,] ### Get the even rows only
### Create new variable Method.New with the value from Method.1 in nsf5B.odd.df and all of the values from Method.2nd in nsf5B.even.df
nsf5B.odd.df$Method.New<-nsf5B.odd.df$Method.1st
nsf5B.even.df$Method.New<-nsf5B.even.df$Method.2nd
nsf5B.final.df<-rbind(nsf5B.odd.df, nsf5B.even.df)  ### Combine the two dfs--nsf5B.odd.df and nsf5B.even.df into one data frame.
nsf5B.final.df$Support_2021<-nsf5B.final.df$Support_2021*0.5 ### Overwrite Suppprt_2021 variable to take 1/2 of the value of each row for Support_2021

### Before re-attaching nsfB to nsfA, we have to add a couple of variables to nsf5A.df
nsf5A.df$Method.1st<-nsf5A.df$Method
nsf5A.df$Method.2nd<-NA               ### Because there is only one method in nsfA.df
nsf5A.df$Method.New<-nsf5A.df$Method  ###This is the same value.
nsf5.df<-rbind(nsf5A.df,nsf5B.final.df) ### Finally, recombine nsf5B.final.df with nsf5A.df to make final nsf5.df

### Aggregate the data across method, and type.
### But, first we have to create a new variable for method name and make it a factor variable

nsf5.df <- nsf5.df %>%
  select(names(nsf5.df)) %>%
  mutate(
    Method.Name= factor(ifelse(Method.New %in% "1", "Quantitative", 
                               ifelse(Method.New %in% "2", "Qualitative",
                                      ifelse(Method.New %in% "4","Experimental", "Formal Theory")))))

#### CREATE THE DATA FRAME FROM WHICH TO PLOT FIGURE 5 
fig5.df <- as.data.frame(nsf5.df)  %>%
  group_by(Method.Name, five.year, .drop=FALSE) %>%
  dplyr::summarise(Total = sum(Support_2021), .groups="drop") %>%
  complete(expand(., Method.Name, five.year), fill = list(Total = 0)) %>%
  fill(Method.Name,five.year)

### Because the above code doesn't work to create zero values for combinations of groups that have zero dollars awarded
### for a specific time period, I've had to do it manually by creating a new 
### data frame with the extra rows, then r-binding that data frame to the existing data frame.
fig5extrarows<-data.frame("Method.Name"=rep(c("Experimental","Formal Theory", "Qualitative","Quantitative"),3), 
                          "five.year"=rep(c(1967.5,1972.5,1982.5),4),
                          "Total"=rep(0,12), stringsAsFactors = TRUE)
fig5zero.df<-rbind(fig5.df, fig5extrarows)
fig5zero.df$Method.Reorder<-fct_relevel(fig5zero.df$Method.Name, "Quantitative","Formal Theory","Experimental","Qualitative") ### Reorder factors

### Now Plotting

fiveyearlabels.all<-c("1961-\n1965 ", "1966-\n1970 ","1971-\n1975 ","1976-\n1980 ","1981-\n1985 ","1986-\n1990 ","1991-\n1995 ",
                      "1996-\n2000 ","2001-\n2005 ","2006-\n2010 ","2011-\n2015 ","2016-\n2020 ")

gg.fig5<- ggplot(fig5zero.df, aes(x=five.year+2.5,y=Total/1000000, group=Method.Reorder, colour=Method.Reorder)) + 
  geom_line(linewidth=0.8) +
  geom_point(size=2, shape=18) +
  labs(x="",y="2021 USD (millions)") +
  scale_x_continuous(limits=c(1965,2020), breaks=seq(1965,2020,5), labels=fiveyearlabels.all) + 
  scale_y_continuous(limits = c(0, 7)) + 
  scale_color_manual(values = rev(wes_palette("IsleofDogs1", n = 4)),
                     labels = c("Quantitative", "Formal Theory", "Experimental", "Qualitative")) + 
  theme_bw() + 
  coord_cartesian(clip = "off") + 
  theme(legend.position = "bottom",
        legend.key.size = unit(0.4, "cm"),
        legend.title = element_blank(),
        legend.text=element_text(size=11),
        legend.box.background = element_rect(color = "black"),
        axis.line=element_line(),
        axis.title.y = element_text(size=10, vjust=3, color='black'),
        axis.title.x = element_text(size=11, color='black'),
        axis.text.x = element_text(color = 'black', size = 10),
        axis.text.y = element_text(size = 10, color='black'),
        plot.background = element_rect(colour="black", linewidth = 0.75),
        plot.title = element_text(face = 2, vjust = 4, hjust = 0, color='black'),
        plot.title.position = "plot",
        plot.caption.position = "plot",
        panel.spacing = unit(1, "lines"),
        plot.margin = margin(20, 20, 20, 20)) +
  geom_line(key_glyph = draw_key_rect, size=0.2)

plot(gg.fig5)
ggsave(gg.fig5, filename="Figure5.png", width=8.5, height=5.7, dpi=300,units="in")  

tiff("Figure5.tiff", units="in", width=8.5, height=5.7, res=300)
plot(gg.fig5)
dev.off()

###
### FIGURE 6 
###

nsf6.df<-nsf.df ### Create a new data frame for this figure
nsf6.df<-nsf6.df[nsf6.df$Type==4,]  ### Keep only Conference / Institutes, i.e., type=4  

keep.method<-c("1","2","4","5","9")    ### Keep only single-method methods--exclude all multi-method types
nsf6.df<-filter(nsf6.df, Method %in% keep.method)   

### Aggregate the data across method, and type.
### First, create a new variable for method name and make it a factor variable

nsf6.df <- nsf6.df %>%
  select(names(nsf6.df)) %>%
  mutate(
    Method.Name= factor(ifelse(Method.New %in% "1", "Quantitative", 
                        ifelse(Method.New %in% "2", "Qualitative",
                        ifelse(Method.New %in% "4","Experimental", 
                        ifelse(Method.New %in% "5","Formal Theory", "EITM"))))))

### Data frame for the figure itself
fig6.df <- as.data.frame(nsf6.df)  %>%
  group_by(Method.Name, five.year, .drop=FALSE) %>%
  dplyr::summarise(Total = sum(Support_2021), .groups="drop") %>%
  complete(expand(., Method.Name, five.year), fill = list(Total = 0)) %>%
  fill(Method.Name,five.year)

fig6extrarows<-data.frame("Method.Name"=c("EITM","Experimental","Formal Theory", "Qualitative",
                                          "Quantitative"), 
                          "five.year"=rep(1962.5,5),
                          "Total"=rep(0,5), stringsAsFactors = TRUE)

fig6.df<-rbind(fig6.df, fig6extrarows)
### Reorder factors
fig6.df$Method.Reorder<-fct_relevel(fig6.df$Method.Name, "Quantitative","Formal Theory","Experimental","Qualitative", "EITM")

gg.fig6<- ggplot(fig6.df, aes(x=five.year+2.5,y=Total/1000000, group=Method.Reorder, colour=Method.Reorder)) + 
  geom_line(linewidth=0.8) +
  geom_point(size=2, shape=18) +
  labs(x="",y="2021 USD (millions)") +
  scale_x_continuous(limits=c(1965,2020), breaks=seq(1965,2020,5), labels=fiveyearlabels.all) + 
  scale_y_continuous(limits = c(0, 4)) + 
  scale_color_manual(values = c("#0F0D0E","#CCBA72","#79402E","#9986A5","#8D8680"), labels = c("Quantitative", "Formal Theory", "Experimental", "Qualitative","EITM")) + 
  theme_bw() + 
  coord_cartesian(clip = "off") + 
  theme(legend.position = "bottom",
        legend.key.size = unit(0.4, "cm"),
        legend.title = element_blank(),
        legend.text=element_text(size=11),
        legend.box.background = element_rect(color = "black"),
        axis.line=element_line(),
        axis.title.y = element_text(size=10, vjust=3, color='black'),
        axis.title.x = element_text(size=11, color='black'),
        axis.text.x = element_text(color = 'black', size = 10),
        axis.text.y = element_text(size = 10, color='black'),
        plot.background = element_rect(colour="black", linewidth = 0.75),
        plot.title = element_text(face = 2, vjust = 4, hjust = 0, color='black'),
        plot.title.position = "plot",
        plot.caption.position = "plot",
        panel.spacing = unit(1, "lines"),
        plot.margin = margin(20, 20, 20, 20)) +
  geom_line(key_glyph = draw_key_rect, size=0.2)
plot(gg.fig6)
ggsave(gg.fig6, filename="Figure6.png", width=8.5, height=5.7, dpi=300,units="in")  

tiff("Figure6.tiff", units="in", width=8.5, height=5.7, res=300)
plot(gg.fig6)
dev.off()

###
### FIGURE 7
### 

nsf7.df<-nsf.df   
nsf7.df<-nsf7.df[nsf7.df$Type==5,] ### Keep only Dissertation Improvement project Types, i.e., type=5  
keep.method<-c("1","2","3","4","5","8","9","10","11","13")  ## Keep only specific methods
nsf7.df<-filter(nsf7.df, Method %in% keep.method) 


nsf7.df <- nsf7.df %>%
  select(names(nsf7.df)) %>%
  mutate(
    Method.Name= factor(ifelse(Method %in% "1", "Quantitative", 
                        ifelse(Method %in% "2", "Qualitative",
                        ifelse(Method %in% "3", "Quantitative & Qualitative",
                        ifelse(Method %in% "4", "Experimental", 
                        ifelse(Method %in% "8", "Quantitative & Experimental", "Other")))))))

fig7.df <- as.data.frame(nsf7.df)  %>%
  group_by(Method.Name, five.year, .drop=FALSE) %>%
  dplyr::summarise(Total = sum(Support_2021), .groups="drop") %>%
  complete(expand(., Method.Name, five.year), fill = list(Total = 0)) %>%
  fill(Method.Name,five.year)

fig7.df$Method.Reorder<-fct_relevel(fig7.df$Method.Name, "Quantitative","Other","Experimental","Qualitative",
                                    "Quantitative & Experimental","Quantitative & Qualitative")

fig7.df <- as.data.frame(nsf7.df)  %>%
  group_by(Method.Name, five.year, .drop=FALSE) %>%
  dplyr::summarise(Total = sum(Support_2021), .groups="drop") %>%
  complete(expand(., Method.Name, five.year), fill = list(Total = 0)) %>%
  fill(Method.Name,five.year)

fig7.df$Method.Reorder<-fct_relevel(fig7.df$Method.Name, "Quantitative","Experimental","Qualitative",
                                    "Quantitative & Experimental","Quantitative & Qualitative","Other")

### Plotting code
fiveyearlabels.diss<-c("1986-\n1990 ","1991-\n1995 ",
                       "1996-\n2000 ","2001-\n2005 ","2006-\n2010 ","2011-\n2015 ","2016-\n2020 ")

gg.fig7<- ggplot(fig7.df, aes(x=five.year+2.5,y=Total/1000000, group=Method.Reorder, colour=Method.Reorder)) + 
  geom_line(linewidth=0.8) + 
  geom_point(size=2, shape=18) +
  labs(x="",y="2021 USD (millions)") +
  scale_x_continuous(limits=c(1990,2020), breaks=seq(1990,2020,5), labels=fiveyearlabels.diss) + 
  scale_y_continuous(limits = c(0, 1)) + 
  scale_color_manual(values = c("#0F0D0E","#79402E","#9986A5","#D9D0D3","#8D8680","#78b7c5")) +
  #                     labels = c("Quantitative","Formal Theory","Experimental","Qualitative",
  #                                "Quantitative & Experimental","Quantitative & Qualitative")) + 
  theme_bw() + 
  coord_cartesian(clip = "off") + 
  theme(legend.position = "bottom",
        #     legend.key.size = unit(0.4, "cm"),
        legend.title = element_blank(),
        legend.text=element_text(size=11),
        legend.box.background = element_rect(color = "black"),
        legend.spacing.y = unit(0.1, "cm"),
        axis.line=element_line(),
        axis.title.y = element_text(size=10, vjust=3, color='black'),
        axis.title.x = element_text(size=11, color='black'),
        axis.text.x = element_text(color = 'black', size = 10),
        axis.text.y = element_text(size = 10, color='black'),
        plot.background = element_rect(colour="black", linewidth = 0.75),
        plot.title = element_text(face = 2, vjust = 4, hjust = 0, color='black'),
        plot.title.position = "plot",
        plot.caption.position = "plot",
        plot.margin = margin(20, 20, 20, 20)) +
  geom_line(key_glyph = draw_key_rect, size=0.1) +
  guides(colour=guide_legend(nrow=2,byrow=TRUE))
plot(gg.fig7)
ggsave(gg.fig7, filename="Figure7.png", width=8.5, height=5.7, dpi=300,units="in")  


tiff("Figure7.tiff", units="in", width=8.5, height=5.7, res=300)
plot(gg.fig7)
dev.off()

###
### FIGURE 8 CODING ###
###

nsf8.df<-nsf.df
nsf8.df<-nsf8.df[nsf8.df$Type==3,] ## Keep only Diversity Training project Types, i.e., type=3  
keep.method<-c("1","6","7") ## Keep only awards 1 (qNt exclusviely--not split methods) and 6 and 7 (no methods at all).
nsf8.df<-filter(nsf8.df, Method %in% keep.method)   ## leaves 2782 awards

### Creating only two methods categories--Quantitative Training and Other Support
nsf8.df <- nsf8.df %>%
  select(names(nsf8.df)) %>%
  mutate(
    Method.Dummy= factor(ifelse(Method %in% "1", "Quantitative Training", "Other Support")))

### THIS NEXT CODE IS TO CREATE THE DATA FROM WHICH TO CODE METHOD.Dummy, AND FIVE.YEAR 
fig8.df <- as.data.frame(nsf8.df)  %>%
  group_by(Method.Dummy, five.year, .drop=FALSE) %>%
  dplyr::summarise(Total = sum(Support_2021), .groups="drop") %>%
  complete(expand(., Method.Dummy, five.year), fill = list(Total = 0)) %>%
  fill(Method.Dummy,five.year)

fig8extrarows<-data.frame("Method.Dummy"=rep(c("Other Support","Quantitative Training"),3), 
                          "five.year"=rep(c(1962.5,1967.5,1977.5),2),
                          "Total"=rep(0,6), stringsAsFactors = TRUE)
fig8zero.df<-rbind(fig8.df, fig8extrarows)
fig8zero.df$Method.Reorder<-fct_relevel(fig8zero.df$Method.Dummy, "Quantitative Training","Other Support")

### Plotting
fiveyearlabels.all<-c("1961-\n1965 ", "1966-\n1970 ","1971-\n1975 ","1976-\n1980 ","1981-\n1985 ","1986-\n1990 ","1991-\n1995 ",
                      "1996-\n2000 ","2001-\n2005 ","2006-\n2010 ","2011-\n2015 ","2016-\n2020 ")

gg.fig8<- ggplot(fig8zero.df, aes(x=five.year+2.5,y=Total/1000000, group=Method.Reorder, colour=Method.Reorder)) + 
  geom_line(linewidth=0.8, alpha=0.75) + 
  geom_point(size=2, shape=18) +
  labs(x="",y="2021 USD (millions)") +
  scale_x_continuous(limits=c(1965,2020), breaks=seq(1965,2020,5), labels=fiveyearlabels.all) + 
  scale_y_continuous(limits = c(0, 2.5)) + 
  scale_color_manual(values = c("#0F0D0E","#78b7c5"), labels = c("Quantitative Training","Other Support")) + 
  theme_bw() + 
  coord_cartesian(clip = "off") + 
  theme(legend.position = "bottom",
        legend.key.size = unit(0.4, "cm"),
        legend.title = element_blank(),
        legend.text=element_text(size=11),
        legend.box.background = element_rect(color = "black"),
        axis.line=element_line(),
        axis.title.y = element_text(size=10, vjust=3, color='black'),
        axis.title.x = element_text(size=11, color='black'),
        axis.text.x = element_text(color = 'black', size = 10),
        axis.text.y = element_text(size = 10, color='black'),
        plot.background = element_rect(colour="black", linewidth = 0.75),
        plot.title = element_text(face = 2, vjust = 4, hjust = 0, color='black'),
        plot.title.position = "plot",
        plot.caption.position = "plot",
        panel.spacing = unit(1, "lines"),
        plot.margin = margin(20, 20, 20, 20)) +
  geom_line(key_glyph = draw_key_rect, size=0.2)
plot(gg.fig8)
ggsave(gg.fig8, filename="Figure8.png", width=8.5, height=5.7, dpi=300,units="in")

tiff("Figure8.tiff", units="in", width=8.5, height=5.7, res=300)
plot(gg.fig8)
dev.off()

###
### FIGURE 9 CODING ###
###
nsf9.df<-nsf.df  ## Create data file for Figure 9 (nsf9.df)
keep.method<-c("1","2","3","4","5","8","9","10","11","13") 
nsf9.df<-filter(nsf9.df, Method %in% keep.method) ## keep only appropriate methods         
nsf9.df<-nsf9.df[nsf9.df$Type<=6,]   ## remove project types that are not applicable

### Create a new variable that uses the method name (not just the number)
nsf9.df <- nsf9.df %>%
  select(names(nsf9.df)) %>%
  mutate(
    Method.Name.Other= ifelse(Method %in% "1", "Quantitative", 
                       ifelse(Method %in% "2", "Qualitative",
                       ifelse(Method %in% "3", "Quantitative &Qualitative",
                       ifelse(Method %in% "4", "Experimental", 
                       ifelse(Method %in% "5", "Formal Theory", 
                       ifelse(Method %in% "8", "Quantitative &Experimental", 
                       ifelse(Method %in% "9", "Quantitative &Formal Theory", 
                       ifelse(Method %in% "10", "Experimental &Formal Theory", "Other")))))))))

### Take the sum of Support_2021 for each Method.Name value
total2021.bymethod <- as.data.frame(nsf9.df) %>%
  group_by(Method.Name.Other) %>%
  dplyr::summarise(Total = sum(Support_2021))

### Sort by total
gg9.df <- total2021.bymethod %>% 
  arrange(Total) %>%
  mutate(Method.Name.Other = factor(Method.Name.Other, unique(Method.Name.Other))) 

options(scipen=9999)  # to prevent scientific notation

## Plot the data using ggplot
gg.fig9 <- ggplot(gg9.df) + aes(x=Method.Name.Other, y=round_any(Total,1000)) +
  geom_segment(aes(x=Method.Name.Other, xend=Method.Name.Other, y=0, yend=Total), linewidth=1.25, color="dodgerblue") +
  geom_point(color="black", size=2.5, alpha=0.75) +
  geom_text(aes(label=scales::comma(round_any(Total,1000))), hjust = -0.2, size=3) +
  scale_y_continuous(limits = c(0, 600000000), breaks=c(0,100000000,200000000,300000000,400000000,500000000,600000000), labels=comma) + 
  scale_x_discrete(labels=function(x){sub("\\&", paste("&", "\n", sep=""), x)}) +
  labs(y="2021 USD",x="") + 
  theme_bw() +
  coord_flip(clip = "off") +
  theme(plot.background = element_rect(colour="black", linewidth = 0.75),
        panel.border = element_blank(),
        axis.line.x = element_line(), 
        axis.line.y = element_line(),
        axis.ticks.y = element_blank(),
        axis.text.y = element_text(color = 'black', size = 11, hjust = 0),
        axis.title.x = element_text(color='black', size = 11, vjust=-4),
        axis.text.x = element_text(color = 'black', size = 10),
        plot.title = element_text(size = 14, face = 2, vjust = 4, hjust = 0),
        plot.caption =element_text(size=10, hjust=0, vjust=-12),
        plot.title.position = "plot",
        plot.caption.position = "plot",
        plot.margin = margin(20, 20, 40, 20)) 

plot(gg.fig9)
ggsave(gg.fig9, filename="Figure9.png", width=8.5, height=5.7, dpi=300,units="in")  

tiff("Figure9.tiff", units="in", width=8.5, height=5.7, res=300)
plot(gg.fig9)
dev.off()